package tech.popsoft.cip.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.popsoft.cip.manage.exception.AppDataPermissionExceptionEnum;
import tech.popsoft.cip.manage.service.ApiAppService;
import tech.popsoft.cip.manage.service.ApiDataPermissionService;
import tech.popsoft.platform.common.base.BaseServiceImpl;
import tech.popsoft.platform.common.exception.CommonException;
import tech.popsoft.platform.common.exception.CustomException;
import tech.popsoft.cip.manage.entity.ApiApp;
import tech.popsoft.cip.manage.entity.ApiDataPermission;
import tech.popsoft.cip.manage.mapper.ApiDataPermissionMapper;

import java.util.List;

/**
 * 数据权限 服务实现类
 *
 * @author wqliu
 * @date 2021-10-20
 */
@Service
public class ApiDataPermissionServiceImpl extends BaseServiceImpl<ApiDataPermissionMapper, ApiDataPermission> implements ApiDataPermissionService {
    @Autowired
    private ApiAppService apiAppService;


    @Override
    public ApiDataPermission init() {
        ApiDataPermission entity = new ApiDataPermission();

        return entity;
    }

    @Override
    protected void beforeAdd(ApiDataPermission entity) {
        //验证不同的应用，不能数据权限角色和业务编码完全一样
        QueryWrapper<ApiDataPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApiDataPermission::getRoleCode, entity.getRoleCode())
                .eq(ApiDataPermission::getBusinessCode, entity.getBusinessCode())
                .ne(ApiDataPermission::getAppId,entity.getAppId());
        int count = count(queryWrapper);
        if (count > 0) {
            throw new CustomException(AppDataPermissionExceptionEnum.DATA_ROLE_AND_BUSINESS_CODE_EXIST);
        }

        //验证是否存在所有属性一致的记录
        queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ApiDataPermission::getAppId,entity.getAppId())
                .eq(ApiDataPermission::getRoleCode, entity.getRoleCode())
                .eq(ApiDataPermission::getBusinessCode, entity.getBusinessCode());
        //只有不为空的时候再比较，不加该条件，数据库中为null，前端传过来的是空串，比较会失败
        if(StringUtils.isNotBlank(entity.getBusinessCodeSecond())){
            queryWrapper.lambda().eq(ApiDataPermission::getBusinessCodeSecond,entity.getBusinessCodeSecond());
        }

        count = count(queryWrapper);
        if (count > 0) {
            throw new CustomException(CommonException.RECORD_EXIST);
        }
    }

    @Override
    protected void beforeModify(ApiDataPermission entity) {
        //验证不同的应用，不能数据权限角色和业务编码完全一样
        QueryWrapper<ApiDataPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApiDataPermission::getRoleCode, entity.getRoleCode())
                .eq(ApiDataPermission::getBusinessCode, entity.getBusinessCode())
                .ne(ApiDataPermission::getAppId,entity.getAppId())
                .ne(ApiDataPermission::getId,entity.getId());
        int count = count(queryWrapper);
        if (count > 0) {
            throw new CustomException(AppDataPermissionExceptionEnum.DATA_ROLE_AND_BUSINESS_CODE_EXIST);
        }

        //验证是否存在所有属性一致的记录
        queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ApiDataPermission::getAppId,entity.getAppId())
                .eq(ApiDataPermission::getRoleCode, entity.getRoleCode())
                .eq(ApiDataPermission::getBusinessCode, entity.getBusinessCode())
                .eq(ApiDataPermission::getBusinessCodeSecond,entity.getBusinessCodeSecond())
                .ne(ApiDataPermission::getId,entity.getId());
        count = count(queryWrapper);
        if (count > 0) {
            throw new CustomException(CommonException.RECORD_EXIST);
        }
    }

    @Override
    public List<ApiDataPermission> getPermissionByRoleCode(String roleCode,String appCode) {
        //通过应用编码获取应用标识
        ApiApp app = apiAppService.getByCode(appCode);
        String appId=app.getId();
        //查找满足条件的数据
        List<ApiDataPermission> list = this.lambdaQuery().eq(ApiDataPermission::getRoleCode, roleCode)
                .eq(ApiDataPermission::getAppId, appId).list();
        return list;
    }
}
